package com.asia5b.a5mall.middleware.logic.viewmodel.member

import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.config.UserRepository
import com.asia5b.a5mall.middleware.model.bean.PostCommentAgainExBean
import com.asia5b.a5mall.middleware.model.bean.PostCommentAgainBean
import com.asia5b.a5mall.middleware.model.bean.PostCommentBean
import com.asia5b.a5mall.middleware.model.bean.PostGoodsBean
import com.asia5b.a5mall.middleware.model.member.CommentsModel
import io.reactivex.Observable
import io.reactivex.functions.Consumer
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

/**
 * Created by zhangyuncai on 2019/12/21.
 * @Note   追加评论逻辑控制器
 */
class CommentAgainActivityVM:SimpleViewModel() {


    val getFirstCommentDetailResultLiveData= MutableLiveData<ArrayList<CommentsModel>>()
    fun getFirstCommentDetail(orderSn: String) {
        val observable = memberApi.getFirstCommentDetail(orderSn)
        baseSubscribe(observable, Consumer { commontsList ->
//            providerView().getFirstCommentDetailResult(commontsList)
            getFirstCommentDetailResultLiveData.value=commontsList
        })
    }

    fun commit(data: PostCommentBean) {
        val uploadImgList = arrayListOf<String>()
        for (postGoodsModel in data.comments) {
            uploadImgList.addAll(postGoodsModel.waitUploadImages)
        }
        val observable = Observable.fromIterable(uploadImgList)
                .flatMap { waitUploadImageFilePath ->
                    uploader(waitUploadImageFilePath)
                            .map { uploadImageResultPath ->
                                /*
                                把本地图片替换成网络图片
                                 */
                                for (postGoodsModel in data.comments) {
                                    if (postGoodsModel.waitUploadImages.contains(waitUploadImageFilePath)) {
                                        postGoodsModel.images.add(uploadImageResultPath)
                                        postGoodsModel.waitUploadImages.remove(waitUploadImageFilePath)
                                    }
                                }
                                uploadImageResultPath
                            }
                }
                .toList()
                .flatMapObservable {
                    memberApi.addCommentAgain2(getModel(data.comments))
                }

        baseSubscribe(observable, Consumer {
//            providerView().commitResult()
            commitResultLiveData.value=true
        })

    }
    val commitResultLiveData=MutableLiveData<Boolean>()

    fun getModel(comments: ArrayList<PostGoodsBean>): ArrayList<PostCommentAgainBean> {
        val list = arrayListOf<PostCommentAgainBean>()
        for (input in comments) {
            val postCommentAgainModel = PostCommentAgainBean()
            val memberId = UserRepository.getUserInfo().member_id
            val seller_id = input.seller_id
            postCommentAgainModel.additional_comment = PostCommentAgainExBean(memberId, seller_id)
            postCommentAgainModel.member_id = memberId
            postCommentAgainModel.seller_id = seller_id
            postCommentAgainModel.comment_id = input.commentId
            postCommentAgainModel.sku_id = input.sku_id
            postCommentAgainModel.content = input.content
            postCommentAgainModel.images = input.images
            list.add(postCommentAgainModel)
        }

        return list
    }


    /**
     * 将图片资料上传至云服务器
     */
    fun uploader(filePath: String): Observable<String> {
        val localFile = File(filePath)
        val localRequestBody = RequestBody.create("image/jpeg".toMediaTypeOrNull(), localFile)
        val localPart = MultipartBody.Part.createFormData("file", localFile.name, localRequestBody)
        return baseApi.uploader(localPart, "")
                .map { uploadImgResModel ->
                    return@map uploadImgResModel.url
                }
    }
}